/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.mochaccino.framework.rest.resources;

import com.google.common.base.Preconditions;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ResourceClasspath implements Resource {
	private List<URL> urlList = new ArrayList<URL>();
	private Logger    logger  = LoggerFactory.getLogger( getClass() );

	public ResourceClasspath( String basePath ) {
		this( ClassLoader.getSystemClassLoader(), basePath );
	}

	public ResourceClasspath( ClassLoader classLoader, String basePath ) {
		try {
			Enumeration<URL> urls = Preconditions.checkNotNull( classLoader ).getResources( Preconditions.checkNotNull( basePath ) );
			if ( urls != null ) {
				if ( urls != null ) {
					while ( urls.hasMoreElements() ) {
						urlList.add( urls.nextElement() );
					}
				}
			}
		} catch ( IOException e ) {
			logger.error( e.getMessage() );
		}
	}

	@Override
	public URL[] getURLs() {
		return urlList.toArray( new URL[urlList.size()] );
	}
}
